/*
 * Java Bittorrent API as its name indicates is a JAVA API that implements the Bittorrent Protocol
 * This project contains two packages:
 * 1. jBittorrentAPI is the "client" part, i.e. it implements all classes needed to publish
 *    files, share them and download them.
 *    This package also contains example classes on how a developer could create new applications.
 * 2. trackerBT is the "tracker" part, i.e. it implements a all classes needed to run
 *    a Bittorrent tracker that coordinates peers exchanges. *
 *
 * Copyright (C) 2007 Baptiste Dubuis, Artificial Intelligence Laboratory, EPFL
 *
 * This file is part of jbittorrentapi-v1.0.zip
 *
 * Java Bittorrent API is free software and a free user study set-up;
 * you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Java Bittorrent API is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Java Bittorrent API; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @version 1.0
 * @author Baptiste Dubuis
 * To contact the author:
 * email: baptiste.dubuis@gmail.com
 *
 * More information about Java Bittorrent API:
 *    http://sourceforge.net/projects/bitext/
 */

package br.biofoco.p2p.rpc.torrent;

import jBittorrentAPI.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Simple example to show how it is possible to publish a .torrent file to a
 * tracker
 */
public class PublisherTracker {

	private static Logger logger = LoggerFactory.getLogger(PublisherTracker.class);
	private String tracker_upload = null;

	public PublisherTracker() {

	}

	public PublisherTracker(Logger log) {
		this();
		if(log != null)
			logger = log;
		loadConfig();
	}

	public void publish(String[] file_torrent) {
		logger.debug("CreateTorrent.publish(): Publishing file on the tracker: " + "*********************");
		logger.debug("CreateTorrent.publish(): TRACKER: " + tracker_upload);
		for (String torrents : file_torrent) {

			try {
				ConnectionManager.publish(torrents, tracker_upload, "none",
						"none", "data", "", "", "7");
			} catch (Exception e) {
				logger
						.debug("PublishTracker.publish(): Error submit torrent file = "
								+ e.getMessage());
				e.printStackTrace();
			}

		}
	}

	private void loadConfig() {

		logger
				.debug("CreateTorrent.loadConfig(): Carregando o arquivo de propriedades...");
		Properties p = new Properties();

		try {
			//p.load(new FileInputStream(File.separator + "eclipse" + File.separator + "work_servcom" + File.separator +
					//"bio-unb" + File.separator + "torrent" + File.separator + "conf" + File.separator + 
					//"torrent.ini"));
			p.load(new FileInputStream(File.separator + System.getProperty("user.home") + File.separator + "dist"
					+ File.separator + "biofoco" + File.separator + "torrent" + File.separator + "conf" 
					+ File.separator + "torrent.ini"));
		} catch (FileNotFoundException e) {
			logger.debug("CreateTorrent.loadConfig(): File not found - "
					+ e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			logger.debug("CreateTorrent.loadConfig(): Open error - "
					+ e.getMessage());
			e.printStackTrace();
		}

		//tracker_upload = System.getProperty("TRACKER_UPLOAD_URL",
				//"http://localhost:8082/upload");
		tracker_upload = "http://164.41.14.27:8082/upload";
		
		System.out.println("TRACKER TRACKER TRACKER ================> " + tracker_upload);


	}

}
